<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/7/17
 * Time: 10:51
 */
namespace app\business\controller;

use think\Db;

class Apilogin extends Base
{
    public function _initialize()
    {

    }



    /**
     * 登录操作
     * @return \think\response\Json
     */
    public function doLogin(){
        extract(input());
        $shop = Db::name('shops')
            ->where('s_phone',$phone)
            ->find();
        if(empty($shop)){
            return json(['status'=>0,'msg'=>'该手机号尚未注册']);
        }
        if($shop['s_sta'] == 2){
            return json(['status'=>0,'msg'=>'该商户已被注销']);
        }
        if($shop['s_password'] != md5($password)){
            return json(['status'=>0,'msg'=>'密码错误']);
        }
        return json(['status' => 200, 'msg' => $shop]);
    }

    /**
     * 找回密码
     * @return mixed|\think\response\Json
     */
    public function resetPwd(){
        extract(input());
        $user = Db::name('shops')
            ->where('s_phone',$phone)
            ->field(['s_id','s_phone','s_sta'])
            ->find();
        if(empty($user)){
            return json(['status'=>0,'msg'=>'该手机号尚未注册']);
        }
        if($user['s_sta'] == 2){
            return json(['status'=>0,'msg'=>'该商户已被注销']);
        }
        $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['status'=>0,'msg'=>'请先发送验证码']);
        }
        //验证密码
        if($pwd != $surepwd) {
            return json(['status' => 0, 'msg' => '两次输入的密码不一致，请重新输入']);
        }
        $t = time() - $d['time2'];
        if($t >= 600){
            return json(['status'=>0,'msg'=>'验证码失效']);
        }
        if($code != $d['code']){
            return json(['status'=>0,'msg'=>'验证码错误']);
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
            Db::name('shops')->where(['s_phone'=>$phone])->update(['s_password'=>md5($pwd),'s_update_time'=>date('Y-m-d H:i:s')]);
            // 提交事务
            Db::commit();
            return json(['status'=>200,'msg'=>'密码重置成功']);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['status'=>0,'msg'=>'密码重置失败']);
        }
    }

    /**
     * 快捷登录
     * @return mixed|\think\response\Json
     */
    public function quickLogin(){
        extract(input());
        $shop = Db::name('shops')
            ->where('s_phone',$phone)->find();

        if(empty($shop)){
            return json(['status'=>0,'msg'=>'该手机号尚未注册']);
        }
        if($shop['s_sta'] == 2){
            return json(['status'=>0,'msg'=>'该商户已被注销']);
        }
        $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['status'=>0,'msg'=>'请先发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 600){
            return json(['status'=>0,'msg'=>'验证码失效']);
        }
        if($code != $d['code']){
            return json(['status'=>0,'msg'=>'验证码错误']);
        }
        Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
        return json(['status'=>200,'msg'=>$shop]);
    }

    /**
     * 注册
     * @return mixed|\think\response\Json
     */
    public function register(){
        extract(input());
        $user = Db::name('shops')
            ->where('s_phone',$phone)
            ->field(['s_id','s_phone'])
            ->find();
        $shop_son = Db::name('role_name')
            ->where('role_user',$phone)
            ->field(['role_status','f_id','r_s_id','role_password'])
            ->find();
        if(!empty($user) || $shop_son){
            return json(['status'=>0,'msg'=>'该手机号已注册']);
        }
        //验证密码
        if($pwd != $surepwd) {
            return json(['status' => 0, 'msg' => '两次输入的密码不一致，请重新输入']);
        }
        $d = Db::name('shop_phonecode')->where(['phone'=>$phone,'status'=>0])->find();
        if(empty($d)){
            return json(['status'=>0,'msg'=>'请先发送验证码']);
        }
        $t = time() - $d['time2'];
        if($t >= 600){
            return json(['status'=>0,'msg'=>'验证码失效']);
        }
        if($code != $d['code']){
            return json(['status'=>0,'msg'=>'验证码错误']);
        }
        // 启动事务
        Db::startTrans();
        try{
            Db::name('shop_phonecode')->where(['phone'=>$phone])->setField ('status',1);
            $token = md5(date('YmdHis') . rand(100000, 999999));
            if(!isset($ex) || !$ex){
                $ex = 86;
            }
            $ins = [
                's_phone'=>$phone,
                's_password'=>md5($pwd),
                's_token'=>$token,
                's_time'=>date('Y-m-d H:i:s'),
                's_num'=>time() . rand(1000,9999),
                's_china'=>$ex
            ];
            $sid = Db::name('shops')->insertGetId($ins);
            $shop = Db::name('shops')
                ->where('s_id',$sid)
                ->find();
            // 提交事务
            Db::commit();
            return json(['status'=>200,'msg'=>$shop]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['status'=>0,'msg'=>'注册失败']);
        }
    }

    /**
     * 发送验证码
     * @return \think\response\Json
     */
    public function sendCode(){
        extract(input());
        $user = Db::name('shops')
            ->where('s_phone',$phone)
            ->field(['s_id','s_phone'])
            ->find();
        $shop_son = Db::name('role_name')
            ->where('role_user',$phone)
            ->field(['role_status','f_id','r_s_id','role_password'])
            ->find();
        //$type == 1代表登录&找回密码&修改密码&注销&换手机第1步  为2代表注册&换手机第2步  3是企业认证发送的
        if(empty($user) && $type == 1){
            return json(['status'=>0,'msg'=>'该手机号尚未注册']);
        }
        if(!empty($user) && $type == 2){
            return json(['status'=>0,'msg'=>'该手机号已注册']);
        }
        if(!empty($shop_son) && $type == 2){
            return json(['status'=>0,'msg'=>'该手机号已注册']);
        }
        $u = Db::name('shop_phonecode')
            ->where('phone',$phone)
            ->find();
        $code = GetRandCode(6);
        if(isset($params['ex']) && $params['ex'] != '86'){
            $phone = '00' . $params['ex'] . $params['phone'];
        }
        $res = YzxSms($code,$phone);
        if($res['code'] == 000000){
            if($type == 3){
                Db::name('shops')->where('s_id',$user_id)->update(['s_code'=>$code,'s_code_time'=>time(),'s_code_status'=>0]);
            }else{
                if(!empty($u)){
                    Db::name('shop_phonecode')->where('phone',$phone)->update(['code'=>$code,'time2'=>time(),'status'=>0]);
                }else{
                    Db::name('shop_phonecode')->insert(['phone'=>$phone,'code'=>$code,'time2'=>time(),'status'=>0]);
                }
            }

            return json(['status'=>200,'msg'=>$code]);
        }else{
            return json(['status'=>0,'msg'=>'验证码发送失败，请重试']);
        }

    }

    /**
     * 用户协议和隐私协议
     * @return \think\response\Json
     */
    public function xieyi()
    {
        $type = $_POST['type'];
        if($type == 1){
            $agreement = Db::table('up_about_us')
                ->where('about_id', 1)
                ->value('shop_agreement');
        }else{
            $agreement = Db::table('up_about_us')
                ->where('about_id', 2)
                ->value('about_us');
        }

        if($agreement) {
            return json(['status' => 200, 'msg' => $agreement]);
        } else {
            return json(['status' => 0, 'msg' => []]);
        }
    }
}